package com.hunter.api.handler;

import com.alibaba.fastjson.JSONObject;

import com.hunter.api.bean.UserBean;
import com.hunter.api.help.result.ErrorHelp;
import com.hunter.api.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @author HunterHuang
 * @version 1.0
 * @ClassName UserTestHandler
 * @date 2019/5/30 12:23
 */
@Service
public class UserHandler {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TokenHandler tokenHandler ;

    /**
     /* 登录
     */
    public JSONObject Login(@RequestBody UserBean user){
        JSONObject json = new JSONObject();
        try{
            user = userMapper.LoginInfo(user);
            if (user!=null) {
                json = tokenHandler.getToken("",user);
                json.put("code", 20000);
                json.put("msg", "登录成功");
                json.put("data", "{}");
            }else{
                json= ErrorHelp.USER_NOT_EXISTS;
            }
        }catch (Exception e){
            json.put("code","500");
            json.put("msg",e.toString());
            json.put("data","{}");
        } finally {
            return json;
        }

    }

    /**
     /* UserInfo
     */
    public JSONObject UserInfo(Long user_id){
        JSONObject json = new JSONObject();
        UserBean user = new UserBean();
        try{
            user = userMapper.userInfo(user_id);
            if (user!=null) {
                json.put("code", 20000);
                json.put("msg", "查询用户信息成功");
                json.put("data", userMapper.Login(user));
            }else{
                json= ErrorHelp.USER_NOT_EXISTS;
            }
        }catch (Exception e){
            json.put("code","500");
            json.put("msg",e.getCause());
            json.put("data","{}");
        } finally {
            return json;
        }
    }



}
